(function($){

    $.fn.typer = function(options){

        var defaults = $.extend({
            search: '',
            replace: [],
            speed: 100,
            delay: 2000
        }, options);
        
        //生成一个length长度的字符串
        var bintext = function(length){
            var text = '';
            for(var $i = 0; $i<=length;$i++) {
                //text = text + Math.floor(Math.random() * 2) //0或1
                text = text + "*";
            }
            console.log(text);
            return text;
        };

        this.each(function(){

            var $this = $(this);
            var $text = $this.data('text');
            var position = 0;

            var indexOf = $text.indexOf( defaults.search ); //查找替换串的开始索引
            var normal = $text.substr(0, indexOf); //非替换子串
            var changer = $text.substr(indexOf, $text.length); //替换子串

            defaults.replace.push(changer); //把替换子串加入到replace数组里

            var interval = setInterval(function(){
                var $bintext = '';

                if( position == indexOf ) {
                    //字符位置 等于 替换串的索引
                    $bintext = bintext(changer.length-1);

                    $this.html( $text.substr(0, normal.length) );
                    $this.append('<span>' + $bintext + '</span>')

                } else if( position > indexOf ) {
                    //字符位置 大于 替换串的索引
                    $bintext = bintext($text.length-1);

                    $this.delay(defaults.speed).find('span').html(
                        changer.substring(0, position - indexOf) +
                        $bintext.substring(position, ($bintext.length))
                    );

                } else if( position < indexOf ) {
                    //字符位置 小于 替换串的索引
                    $bintext = bintext($text.length-1);

                    $this.delay(defaults.speed).html(
                        normal.substring(0, position) +
                        $bintext.substring(position, ($bintext.length))
                    );

                }

                if( position < $text.length ) {
                    position++;
                } else {
                    clearInterval(interval);

                    var index = 0;
                    setInterval(
                        function(){
                            var position = 0;
                            var newText = defaults.replace[index]; //新的替换文本

                            var changeInterval = setInterval(
                                function(){

                                    var $bintext = bintext(newText.length-1);

                                    $this.delay(defaults.speed).find('span').html(
                                        newText.substring(0, position) +
                                        $bintext.substring(position, ($bintext.length))
                                    );

                                    if( position < $text.length ) {
                                        position++;
                                    } else {
                                        clearInterval(changeInterval);
                                    }

                                }, 
                            defaults.speed);

                            if( index < defaults.replace.length-1 ) {
                                index++;
                            } else {
                                index = 0;
                            }
                        }, 
                    defaults.delay)

                }
            }, defaults.speed)

        });

    }

})(jQuery)

$(function(){
  $('#text').typer({
    search: 'stronger',
    replace: ['powerful', 'easiest', 'stranger']
  })
})